﻿namespace Calendar
{
    using System;
    using System.Drawing;
    using System.Windows.Forms;

    public abstract class AbstractRenderer : IDisposable
    {
        private Font hourFont;
        private Font minuteFont;

        protected AbstractRenderer()
        {
        }

        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool mainThread)
        {
            if (this.hourFont != null)
            {
                this.hourFont.Dispose();
            }
            if (this.minuteFont != null)
            {
                this.minuteFont.Dispose();
            }
        }

        public void DrawAllDayBackground(Graphics g, Rectangle rect)
        {
            if (g == null)
            {
                throw new ArgumentNullException("g");
            }
            using (Brush brush = new SolidBrush(InterpolateColors(this.BackColor, Color.Black, 0.5f)))
            {
                g.FillRectangle(brush, rect);
            }
        }

        public abstract void DrawAppointment(Graphics g, Rectangle rect, Appointment appointment, bool isSelected, Rectangle gripRect);
        public abstract void DrawDayBackground(Graphics g, Rectangle rect);
        public virtual void DrawDayGripper(Graphics g, Rectangle rect, int gripWidth)
        {
            if (g == null)
            {
                throw new ArgumentNullException("g");
            }
            using (Brush brush = new SolidBrush(Color.White))
            {
                g.FillRectangle(brush, rect.Left, rect.Top - 1, gripWidth, rect.Height);
            }
            using (Pen pen = new Pen(Color.Black))
            {
                g.DrawRectangle(pen, rect.Left, rect.Top - 1, gripWidth, rect.Height);
            }
        }

        public abstract void DrawDayHeader(Graphics g, Rectangle rect, DateTime date);
        public abstract void DrawHourLabel(Graphics g, Rectangle rect, int hour, bool ampm);
        public virtual void DrawHourRange(Graphics g, Rectangle rect, bool drawBorder, bool hilight)
        {
            if (g == null)
            {
                throw new ArgumentNullException("g");
            }
            using (SolidBrush brush = new SolidBrush(hilight ? this.SelectionColor : this.WorkingHourColor))
            {
                g.FillRectangle(brush, rect);
            }
            if (drawBorder)
            {
                g.DrawRectangle(SystemPens.WindowFrame, rect);
            }
        }

        public abstract void DrawMinuteLine(Graphics g, Rectangle rect);
        ~AbstractRenderer()
        {
            this.Dispose(false);
        }

        public static Color InterpolateColors(Color color1, Color color2, float percentage)
        {
            int r = color1.R;
            int g = color1.G;
            int b = color1.B;
            int num4 = color2.R;
            int num5 = color2.G;
            int num6 = color2.B;
            byte red = Convert.ToByte((float) (r + ((num4 - r) * percentage)));
            byte green = Convert.ToByte((float) (g + ((num5 - g) * percentage)));
            byte blue = Convert.ToByte((float) (b + ((num6 - b) * percentage)));
            return Color.FromArgb(red, green, blue);
        }

        public virtual Color AllDayEventsBackColor
        {
            get
            {
                return InterpolateColors(this.BackColor, Color.Black, 0.5f);
            }
        }

        public virtual Color BackColor
        {
            get
            {
                return SystemColors.Control;
            }
        }

        public virtual Font BaseFont
        {
            get
            {
                return Control.DefaultFont;
            }
        }

        public virtual Color HalfHourSeperatorColor
        {
            get
            {
                return Color.FromArgb(0xf3, 0xe4, 0xb1);
            }
        }

        public virtual Color HourColor
        {
            get
            {
                return Color.FromArgb(0xff, 0xf4, 0xbc);
            }
        }

        public virtual Font HourFont
        {
            get
            {
                if (this.hourFont == null)
                {
                    this.hourFont = new Font(this.BaseFont.FontFamily, 12f, FontStyle.Regular);
                }
                return this.hourFont;
            }
        }

        public virtual Color HourSeperatorColor
        {
            get
            {
                return Color.FromArgb(0xea, 0xd0, 0x98);
            }
        }

        public virtual Font MinuteFont
        {
            get
            {
                if (this.minuteFont == null)
                {
                    this.minuteFont = new Font(this.BaseFont.FontFamily, 8f, FontStyle.Regular);
                }
                return this.minuteFont;
            }
        }

        public virtual Color SelectionColor
        {
            get
            {
                return SystemColors.Highlight;
            }
        }

        public virtual Color WorkingHourColor
        {
            get
            {
                return Color.FromArgb(0xff, 0xff, 0xd5);
            }
        }
    }
}

